//
//  main.m
//  5NSArray
//
//  Created by zhengwei on 2/3/16.
//  Copyright © 2016 zhengwei. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
       
//      1 数组的初始化
        NSArray *array1 = [[NSArray alloc]init];
        NSArray *array2 = [[NSArray alloc]initWithObjects:@"abc",@"123",@"d",nil];
        NSArray *array3 = @[@"333",@"abc",@"606"];
        
//        NSLog(@"array1=%@ array2=%@ array3=%@",array1,array2,array3);
//      2 增删改查
//        查：位置索引
        NSString *item1 = [array2 firstObject];
        NSString *item2 = [array2 lastObject];
        NSString *item3 = [array2 objectAtIndex:1];
        NSLog(@"%@ %@ %@",item1,item2,item3);
        
        
//        查：内容索引
        NSInteger index1 = [array3 indexOfObject:@"606ed"];
        NSInteger arr3num = (unsigned long)array3.count;
        NSLog(@"arr3num=%lu",arr3num);
        if( index1 >=0 && index1<= arr3num){
            NSLog(@"内容在数组里");
        }else{
            NSLog(@"内容不在数组里");
        }
        NSLog(@"当前内容位置index1=%ld",(long)index1);
        
        
        
//        查：长度
        NSLog(@"cout=%lu",(unsigned long)array2.count);
        
        //查询当前数组是否包含元素，返回1 /0；
        NSLog(@"%d %d",[array2 containsObject:@"123"],[array2 containsObject:@"kij"]);
        
        
        
        /*
         先读取再比较
         */
        //获取NSUserDefaults实例
        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
        
        //读取数组值
        NSMutableArray *MACArray = [userDefaults objectForKey:@"MACArray"];
        NSLog(@"第一次读取的值MACArray value is:%@",MACArray);
        
        NSString *str1 = @"6001949895f3";
        NSRange rang;
        rang.length = 6;
        rang.location = 6;
        NSString *str2 = [str1 substringWithRange:rang];
        NSLog(@"%@",str2);
        
        
        
//        if(MACArray == NULL){
//            NSMutableArray *MACArray = [[NSMutableArray alloc]init];
//            NSLog(@"新建MACArray");
//            NSLog(@"MACArray value is:%@",MACArray);
//
//            if(![MACArray containsObject:str2]){
//                [MACArray addObject:str2];
//                NSLog(@"增加新参数1");
//                NSLog(@"%@",MACArray);
//            }else{
//                NSLog(@"内容在数组里，不添加");
//            }
//
//            //存储数组值
//            [userDefaults setObject:MACArray forKey:@"MACArray"];
//            //        //删除数组值
//            //[userDefaults removeObjectForKey:@"MACArray"];
//            NSLog(@"存储到PRE的数组  %@",MACArray);
//            //同步数据
//            [userDefaults synchronize];
//
//        }
        
        
            
            NSLog(@"MACArray222 value is:%@",MACArray);
        
            [MACArray addObject:@"456"];
//            if(![MACArray containsObject:str2]){
//                [MACArray addObject:@"115"];
//                NSLog(@"增加新参数2");
//                NSLog(@"%@",MACArray);
//            }else{
//                NSLog(@"内容在数组里，不添加");
//            }
            
             //这里添加保存
            //存储数组值
            [userDefaults setObject:MACArray forKey:@"MACArray"];
            //        //删除数组值
            //[userDefaults removeObjectForKey:@"MACArray"];
            NSLog(@"存储到PRE的数组  %@",MACArray);
            //同步数据
            [userDefaults synchronize];
           
        
       
    
        
        
        
         /*
//        增：
        NSMutableArray *array1 = [[NSMutableArray alloc]init];
        NSMutableArray *array2 = [[NSMutableArray alloc]initWithObjects:@"abc",@"123",@"ff",nil];
        [array2 addObject:@"456"];
        [array2 insertObject:@"00" atIndex:2];
        NSLog(@"%@",array2);
//        删除 修改
        [array2 removeLastObject];
        [array2 removeObject:@"00"];
        [array2 removeObjectAtIndex:1];
        [array2 removeAllObjects];
        NSLog(@"%@",array2);
         
//      3 数据遍历和快速枚举:中文
        NSArray *array1 = @[@"abc",@"123",@"你好"];
        NSLog(@"%@",array1);
//        for (int i = 0; i < array1.count; i++) {
//            NSString *item = [array1 objectAtIndex:i];
//            NSLog(@"item=%@",item);
//        }
        for (NSString *item in array1) {
            NSLog(@"item=%@",item);
        }
         
//      4 NSString <->NSArray
        NSString *string = @"1@2@3@4@5";
        NSArray *array1 = [string componentsSeparatedByString:@"@"];
        NSLog(@"%@",array1);
        NSMutableString *string2 = [[NSMutableString alloc]init];
        for (NSString *item in array1) {
            [string2 appendFormat:@"%@@",item];
        }
        NSLog(@"%@",string2);
         */
        /*
//       5 NsmutableArray
        NSArray *array1 = @[@"123",@"abc"];
//        NSMutableArray *array2 = array1;
        NSMutableArray *array2 = [[NSMutableArray alloc]initWithArray:array1];
        NSLog(@"array2=%@",array2);
        NSArray *array3 = array2;
        NSLog(@"%@",array3);
         */
        
    }
    return 0;
}
